<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model\LazyCommon;
use App\Base\BaseModel;
use BaseExt\MyCurl;
use App\Model\LazyCommon\ParentsModel;

class MiniAppModel extends BaseModel{


    public $AppId = 'wxff00091683a6b15c'; //服务号APPId
    public $AppSecret = '2a58ec9ac2064741b3bf78196c640df4'; //服务号APPId

    public $Code;
    public $AccessToken;
    public $SessionKey;
    public $JsApiTicket;
    public $Openid; //商户appid下，用户的openid
    public $url;//请求连接地址
    public $curl_timeout;//请求超时时间
    public $response;//接口返回信息



    public function getOpenidAndAccessToken4Mini($code)
    {
        $this->Code = $code;
        $APPID = $this->AppId;
        $SECRET = $this->AppSecret;
        $CODE = $this->Code;
        $url = "https://api.weixin.qq.com/sns/jscode2session?";
        $url .= "appid=$APPID";
        $url .= "&secret=$SECRET";
        $url .= "&js_code=$CODE&grant_type=authorization_code";
        $return = $this->myCurl($url);//这些接口返回的都是字符串形式
        $return = json_decode($return,true);
        if (!isset($return["openid"])) {
            //code有可能被使用了，需要重新定向到原始页面
            return false;
        }
        $this->SessionKey = $return["session_key"];
        $this->Openid = $return["openid"];
        return true;
    }



    public function myCurl($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
//        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;    //返回json对象
    }



}